package com.ruoyi.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.entity.Carousel;
import com.ruoyi.common.entity.WechatQuryDto;
import com.ruoyi.common.mapper.WebSysConfigMapper;
import com.ruoyi.common.service.CarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.time.LocalDate;
import java.util.List;

/**
 * @author 0617
 * @version 1.0
 * @package:com.ruoyi.common.service.impl
 * @date 2025/8/23 上午10:04
 * @description: TODO
 */
@Service
public class CarouselServiceImpl  implements CarouselService {

    @Autowired
    private WebSysConfigMapper webSysConfigMapper;
    @Override
    public List<Carousel> listCarousel(String query) {
        List<Carousel> carousels = webSysConfigMapper.selectList(Wrappers
                .<Carousel>lambdaQuery()
                .eq(Carousel::getConfigKey, Constants.BANNER));
        return carousels;
    }

    @Override
    public Carousel getCarouselById(Long id) {
        Carousel carousel = webSysConfigMapper.selectOne(Wrappers.<Carousel>lambdaQuery()
                .eq(Carousel::getConfigId, id));
        if (ObjectUtil.isNull(carousel)){
            throw new RuntimeException("轮播图不存在");
        }
        return carousel;
    }

    @Override
    public void saveCarousel(Carousel carousel) {
        carousel.setConfigKey(Constants.BANNER);
        carousel.setConfigType("N");
        carousel.setCreateTime(LocalDate.now());
        int insert = webSysConfigMapper.insert(carousel);
    }

    @Override
    public void updateCarousel(Carousel carousel) {
        int i = webSysConfigMapper.update(carousel,Wrappers
                .<Carousel>lambdaUpdate().eq(Carousel::getConfigId,carousel.getConfigId()));
    }

    @Override
    public void deleteCarousel(Long id) {
        webSysConfigMapper.delete(Wrappers
                .<Carousel>lambdaQuery().eq(Carousel::getConfigId,id));
    }

    @Override
    public List<Carousel> listWechatQr(WechatQuryDto query) {
        List<Carousel> carousels = webSysConfigMapper.selectList(Wrappers
                .<Carousel>lambdaQuery()
                .eq(Carousel::getConfigKey, Constants.WECHATQR));
        return carousels;
    }

    @Override
    public void saveWeChatQr(Carousel carousel) {
        carousel.setConfigKey(Constants.WECHATQR);
        carousel.setConfigType("N");
        carousel.setCreateTime(LocalDate.now());
        int insert = webSysConfigMapper.insert(carousel);
    }

    @Override
    public String getQrcode(String sceneStr) {
        Carousel carousel = webSysConfigMapper.selectOne(Wrappers.<Carousel>lambdaQuery()
                .eq(Carousel::getSceneStr, sceneStr)
                .select(Carousel::getConfigValue)
        );
        return carousel.getConfigValue();
    }

    @Override
    public String getNoticeText() {
        Carousel carousel = webSysConfigMapper.selectOne(Wrappers.<Carousel>lambdaQuery()
                .eq(Carousel::getConfigKey, Constants.NOTICE)
                .select(Carousel::getConfigValue)
        );
        return carousel.getConfigValue();
    }


}
